Skip to content

feature(symfony): Add CSS Color Schema Restriction for Property Validation #7215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

SebLevDev
Copy link
Contributor

@SebLevDev SebLevDev commented Jun 13, 2025

Description

This PR introduces the PropertySchemaCssColorRestriction class, which generates a regular expression (pattern) for validating CSS color values in API Platform property schemas, based on the Symfony CssColor constraint.

Technical Details

Supports multiple CSS color formats (hex, rgb, rgba, hsl, hsla, named colors, etc.).
Dynamically builds a single regex pattern combining all allowed formats, suitable for OpenAPI/JSON Schema documentation.

Example

With a given resource Theme

#[ApiResource]
class Theme
{
    private ?int $id = null;

    #[Assert\NotNull]
    #[Assert\CssColor(
        formats: Assert\CssColor::HEX_LONG,
    )]
    public ?string $backgroundColor = null;
}

The impact in the OpenApi Schema provide

{
  "Theme": {
    "type": "object",
    "properties": {
      "id": {
        "type": "integer",
        "readOnly": true
      },
      "backgroundColor": {
        "type": "string",
        "pattern": "^#(?:[0-9a-f]{6})$",
      }
    },
    "required": ["backgroundColor"]
  }
}

@SebLevDev SebLevDev force-pushed the feature/property-schema-css-color-restriction branch from 7581d75 to 89a2157 Compare June 14, 2025 09:00
@soyuka
Copy link
Member

soyuka commented Jun 26, 2025

looks nice, could you maybe explain how this is used? (or open a doc PR)

It'd be nice to add some tests (can also be functional testing)

@SebLevDev
Copy link
Contributor Author

I've updated the PR description to include an example of how to use the constraint in an entity.
I've also added a unit test to cover the PropertySchemaCssColorRestriction logic.
Let me know if anything else is needed!

@soyuka soyuka merged commit 4abf17b into api-platform:main Jun 27, 2025
110 of 114 checks passed
@soyuka
Copy link
Member

soyuka commented Jun 27, 2025

awesome thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants